/*
 * @lc app=leetcode id=165 lang=cpp
 *
 * [165] Compare Version Numbers
 */
class Solution {
    const char dot = '.';
public:
    int compareVersion(string version1, string version2) {

        int l1 = version1.size();
        int l2 = version2.size();

        int i = 0, j = 0;
        size_t p1, p2;
        while (i < l1 || j < l2) {
            int v1 = 0, v2 = 0;
            if (i < l1) {
                p1 = version1.find(dot, i);
                if (p1 != string::npos) {
                    v1 = atoi(version1.substr(i, p1-i).c_str());
                    i = p1+1;
                } else {
                    v1 = atoi(version1.substr(i).c_str());
                    i = l1;
                }
            }

            if (j < l2) {
                p2 = version2.find(dot, j);
                if (p2 != string::npos) {
                    v2 = atoi(version2.substr(j, p2-j).c_str());
                    j = p2+1;
                } else {
                    v2 = atoi(version2.substr(j).c_str());
                    j = l2;
                }
            }

            if (v1 < v2) {
                return -1;
            } else if (v1 > v2) {
                return 1;
            }
        }

        return 0;
    }
};

